package single_game.gui;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import data.Cyclist;
import data.Team;

public class SingleGameTeamSelect implements ActionListener {
	
	private ArrayList<Cyclist> cyclists;
	private ArrayList<Team> teams;
	private GridBagLayout layout;
	private JPanel panel;
	private JButton[] teamButtons;
	private JLabel[][] teamRiders;
	private JFrame frame;
	
	public SingleGameTeamSelect(JFrame frame, ArrayList<Cyclist> cyclists, ArrayList<Team> teams) {
		
		this.cyclists = cyclists;
		this.teams = teams;
		this.frame = frame;
		
		frame.getContentPane().removeAll();
		frame.getContentPane().revalidate();	
		frame.getContentPane().repaint();		
		frame.setTitle("Cycling Manager - Single Game team selection");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new GridLayout(0,1));
		
		layout = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets(0,0,2,0);
		
		layout.columnWidths = new int[] { 200, 200, 200, 200 };
		layout.rowHeights = new int[] {};
		
		panel = new JPanel(layout);
		teamButtons = new JButton[teams.size()];
		teamRiders = new JLabel[teams.size()*3][2];
		
		int y = 0, x;
		
		for (int i = 0; i < teams.size(); i++) {
			c.gridwidth = 2;
			
			if (i%2 == 0) {
				c.gridx = 0;
				x = 0;
				if (i > 0)
					y += 4;
			}
				
			else {
				c.gridx = 2;
				x = 2;
			}
				
			c.gridy = y;
			teamButtons[i] = new JButton(teams.get(i).getTeamName());
			teamButtons[i].setOpaque(true);
			teamButtons[i].addActionListener(this);
			panel.add(teamButtons[i], c);
			
			c.gridwidth = 1;
			c.gridy = y+1;
			teamRiders[i*2][0] = new JLabel("Name");
			teamRiders[i*2][0].setOpaque(true);
			teamRiders[i*2][0].setBackground(Color.yellow);
			panel.add(teamRiders[i*2][0], c);
			
			c.gridx = x + 1;
			teamRiders[i*2][1] = new JLabel("MO HI SP CO");
			teamRiders[i*2][1].setOpaque(true);
			teamRiders[i*2][1].setBackground(Color.yellow);
			panel.add(teamRiders[i*2][1], c);
			
			c.gridy = y+2;
			c.gridx = x;
			teamRiders[i*2+1][0] = new JLabel(setName(cyclists.get(teams.get(i).teamRiders.get(0))));
			teamRiders[i*2+1][0].setOpaque(true);
			panel.add(teamRiders[i*2+1][0], c);
			
			c.gridx = x + 1;
			teamRiders[i*2+1][1] = new JLabel(setStats(cyclists.get(teams.get(i).teamRiders.get(0))));
			teamRiders[i*2+1][1].setOpaque(true);
			panel.add(teamRiders[i*2+1][1], c);
			
			c.gridx = x;
			c.gridy = y+3;
			teamRiders[i*2+2][0] = new JLabel(setName(cyclists.get(teams.get(i).teamRiders.get(1))));
			teamRiders[i*2+2][0].setOpaque(true);
			panel.add(teamRiders[i*2+2][0], c);
			
			c.gridx = x + 1;
			teamRiders[i*2+2][1] = new JLabel(setStats(cyclists.get(teams.get(i).teamRiders.get(1))));
			teamRiders[i*2+2][1].setOpaque(true);
			panel.add(teamRiders[i*2+2][1], c);
			
		}
		
		frame.add(panel);
		
	}
	
	
	private String setStats(Cyclist tmp) {
		String result = tmp.getMountain()+" "+tmp.getHill()+" "+tmp.getSprint()+" "+tmp.getCobble();
		return result;
	}
	
	private String setName(Cyclist tmp) {
		String result = tmp.getSurname()+" "+tmp.getFirstName();
		return result;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		
		int teamId = getTeamIdByName(((AbstractButton) e.getSource()).getText());
		if (teamId != -1)
			new SingleGameRidersSelect(frame, teamId, teams, cyclists);
		else
			System.out.println("Choosing team error...");
		
	}
	
	private int getTeamIdByName(String teamName) {
		for (int i = 0; i < teams.size(); i++) {
			if (teamName.equals(teams.get(i).getTeamName())) {
				return i;
			}
		}
		return -1;
	}
	
}
